from drf_yasg import openapi
from .serializers import *

AuthViewsetDocs = {
    'login': {
        'operation_summary': "用户登录接口",
        'request_body': LoginSerializer,
        'responses': {
            200: openapi.Response(
                description="登录成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                        "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
                    }
                }
            ),
            401: openapi.Response(
                description="用户名或密码错误",
                examples={
                    "application/json": {
                        "status": "failed",
                        "message": "用户名或密码错误"
                    }
                }
            ),
            400: "请求参数错误"
        }
    },
    'logout': {
        'operation_summary':"退出登录接口",
        'operation_description':"通过刷新令牌退出登录。需要接收前端传来的refresh token。",
        'request_body': openapi.Schema(
        type=openapi.TYPE_OBJECT,
        properties={
            'refresh': openapi.Schema(type=openapi.TYPE_STRING)
        },
        required=['refresh']
    ),
        'responses':{
            200: openapi.Response(
                description="退出成功"
            ),
            400: "请求参数错误"
        }
    },
    'register': {
        'operation_summary': "用户注册接口",
        'request_body': openapi.Schema(
        type=openapi.TYPE_OBJECT,
        required=['username', 'password', 'phone', 'captcha'],  # captcha是必需的
        properties={
            'username': openapi.Schema(
                type=openapi.TYPE_STRING,
                description='用户名'
            ),
            'password': openapi.Schema(
                type=openapi.TYPE_STRING,
                description='密码'
            ),
            'phone': openapi.Schema(
                type=openapi.TYPE_STRING,
                description='手机号'
            ),
            'email': openapi.Schema(
                type=openapi.TYPE_STRING,
                description='邮箱(可选)',
                required=['email']
            ),
            'captcha': openapi.Schema(
                type=openapi.TYPE_STRING,
                description='验证码'
            )
        }
    ),
        'responses': {
            201: openapi.Response(
                description="注册成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "message": "注册成功"
                    }
                }
            ),
            400: "请求参数错误"
        }
    },
'send_code': {
        'operation_summary': "发送验证码接口",
        'operation_description':"通过手机号或邮箱发送验证码，场景类型：register（注册）、reset_pwd（找回密码）",
        'request_body': SendCodeSerializer,
        'responses': {
            200: openapi.Response(
                description="验证码发送成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "message": "验证码已发送",
                        "dev_code": "123456"
                    }
                }
            ),
            400: openapi.Response(
                description="请求错误",
                examples={
                    "application/json": [
                        {"status": "failed", "message": "注册场景必须提供手机号"},
                        {"status": "failed", "message": "只能选择一种找回方式"}
                    ]
                }
            )
        }
    },
'check_code': {
        'operation_summary': "校验验证码接口",
        'operation_description':"验证手机号或邮箱的验证码是否正确,前端只有重置密码的时候才会返回token，注册的验证码不会返回",
        'request_body': VerifyCodeSerializer,
        'responses': {
            200: openapi.Response(
                description="验证码校验成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "token": "xxx"
                    }
                }
            ),
            400: openapi.Response(
                description="验证码错误或已过期",
                examples={
                    "application/json": {
                        "status": "failed",
                        "message": "验证码错误或已过期"
                    }
                }
            )
        }
    },
'reset_password': {
        'operation_summary': "重置密码接口",
        'operation_description':"使用token和验证码重置密码，token从请求头Authorization字段提取",
        'request_body': ResetPasswordSerializer,
        'responses': {
            200: openapi.Response(
                description="重置密码成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "message": "密码重置成功"
                    }
                }
            ),
            400: openapi.Response(
                description="无效的token或验证码",
                examples={
                    "application/json": {
                        "status": "failed",
                        "message": "无效的token或验证码"
                    }
                }
            )
        }
    },
'upload_avatar':{
        'operation_summary':"头像上传接口",
        'operation_description':""""
        上传用户头像到七牛云存储并更新用户头像URL

        请求要求：
        - 必须携带有效的JWT Token
        - 必须使用multipart/form-data格式
        - 文件字段名必须为'avatar'
        - 文件大小不超过2MB
        - 仅支持JPG/PNG/GIF图片格式
        
        成功响应：
        - 自动删除用户旧头像(如果存在)
        - 返回新头像的完整访问URL
        - 更新用户模型的avatar_key字段
        
        权限要求：已登录用户
        """,
        'request_body':openapi.Schema(
        type=openapi.TYPE_OBJECT,
        properties={
            'avatar': openapi.Schema(
                type=openapi.TYPE_FILE,
                description='头像图片文件'
            )
        },
        required=['avatar']
        ),

        'responses':{
            200: openapi.Response(
            description="上传成功",
            examples={
                "application/json": {
                    "status": "success",
                    "avatar_url": "http://qiniu.com/avatars/uid_123.jpg"
                }
            }
            ),
            400: openapi.Response(
                description="参数错误",
                examples={
                    "application/json": [
                        {"status": "failed", "message": "未提供头像文件"},
                        {"status": "failed", "message": "头像文件不能超过2MB"},
                        {"status": "failed", "message": "仅支持图片文件"}
                    ]
                }
            ),
            401: openapi.Response(description="未授权",
            examples={
                "application/json": {
                    "status": "failed",
                    "message": "身份认证失败"
                }
            }),
        }
    },
    'refresh_token':{
        'operation_summary':"刷新token接口",
        'operation_description':"通过刷新令牌刷新访问令牌。前端需要刷新token的时候调用此接口",
        'responses':{
            200: openapi.Response(
                description="刷新token成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
                        "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
                    }
                }
            ),
            400: "无效的刷新令牌"
        }
},
'get_user_info':{
    'operation_summary':"获取当前用户信息",
        'operation_description':"用于前端个人中心加载已登录用户的用户名、头像和uid",
        'responses':{
            200: openapi.Response(
                description="获取成功",
                examples={
                    "application/json": {
                        "username": "testuser",
                        "uid": "123456",
                        "avatar_url": "http://example.com/avatar.jpg"
                        # 移除了email和phone等敏感信息
                    }
                }
                ),
                401: openapi.Response(description="未授权")
        }
},
'reset_phone_or_email':{
    'operation_summary':"修改手机号或邮箱接口",
        'operation_description':"""
        修改用户的手机号或邮箱，不支持同时修改。
        """,
        'request_body':ResetPhoneOrEmailSerializer,
        'responses':{
            200: openapi.Response(
                description="修改成功",
                examples={
                    "application/json": {
                        "status": "success",
                        "message": "修改成功"
                    }
                }
            ), 
            400: openapi.Response(
                description="参数错误",
                examples={
                    "application/json": [
                        {"status": "failed", "message": "至少提供一个新的邮箱或手机号"},
                        {"status": "failed", "message": "一次只能改一个"}
                    ]
                }
            ),
        } 
},
'get_avatar_url':{
'operation_summary':"平台首页获取头像URL的接口",
        'operation_description':"""
        获取用户头像的URL，支持过期时间设置。
        注意个人中心头像获取不是用该接口""",
        'responses':{
            200: openapi.Response(
                description="获取成功", 
            ) 
        }
}
}